<?php
/**
 * This file is part from Ewuroweb Shop
 *
 * @package EW
 * @subpackage Core
 * @author Ivan I. Atanasov <i.atanasov@viscomp.bg>
 */


/**
 * 
 * @author Ivan I. Atanasov <i.atanasov@viscomp.bg>
 *
 */
class EW_Core {
    static function showDebug() {
        $debug = array();
        $bt = debug_backtrace();
        foreach($bt as $b) {
            $debug[] = array(
                'file' => $b['file'],
                'class' => $b['class'],
                'function' => $b['function'],
                'line' => $b['line']
                );
        }
        
        echo '<pre>';
        var_dump($debug);
        echo '</pre>';
    }
    
    /**
     * get_called_class() implementation for PHP versions older than 5.3.0
     */
    static function getCalledClass() { 
        /* Instances per Line */
        $i = Zend_Registry::isRegistered('EW_Core_i') ? Zend_Registry::get('EwCore_i') : 0;
        
        /* Current Line Number */
        $fl = Zend_Registry::isRegistered('EW_Core_fl') ? Zend_Registry::get('EwCore_fl') : null;
        
        $bt = debug_backtrace();
        //self::showDebug(); die();
        
        /** E ta tapotiq ne e konstatnta
         * 
         * @var int	Index of the Element of Back-Trace Array
         */
        $p = 1;
        /*
        for($r = 0; $i < count($bt); $r++) {
            if(($bt[$r]['class']=='EW_Core') && ($bt[$r]['function']=='getCalledClass')) {
                $p = $r+2;
                break;
            }
        }
       */
        /*
         * If there is more than one calls per row.
         * Example: anyFunction(Class::method(), OtherClass::otherMethod())
         */
        if($fl == $bt[$p]['file'].$bt[$p]['line']) {
            $i++;
        }
        else {
            $i = 0;
            if(file_exists($bt[$p]['file'])) {
                $fl = $bt[$p]['file'].$bt[$p]['line'];
            }
            else {
                $fl = $bt[$p+1]['file'].$bt[$p]['line'];
            }
            
        }
        die(var_dump($fl));
        $lines = file($bt[$p]['file']);

        preg_match_all(
            '/([a-zA-Z0-9\_]+)::'.$bt[$p]['function'].'/',
            $lines[$bt[$p]['line']-1],
            $matches
        );
        
        
        Zend_Registry::set('EW_Core_i', $i);
        Zend_Registry::set('EW_Core_fl', $fl);
        
    	/* 
         * The next code fix If Any Child Class call static method from your parent such parent::method();
         * 
         * It's provided an option if there is defined two classes in one file.
         */
        if($matches[1][$i] == 'parent') {
            for($nl=0; $nl<count($lines); $nl++) {
                if(preg_match('/^([\s]*)class (\w+)/', $lines[$nl], $matches2)) {
                    $currentClass = $matches2[2];
                }
                
                if(($nl == $bt[$p]['line']) && isset($currentClass)) {
                    return $currentClass;
                }
            }
        }
        
        return $matches[1][$i];
    }
    //===============================================================================================================
    
}
